package com.yehongyu.mytools.core.reflect;

import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class Reflections {

	/**
	 * 构造函数显示
	 * 
	 * @param c1
	 */
	public static void printConstructors(Class<?> c1) {
		Constructor<?>[] constructors = c1.getDeclaredConstructors();
		System.out
				.println("------------------print Constructors-----------------");

		for (int i = 0; i < constructors.length; i++) {
			Constructor<?> c = constructors[i];
			String name = c.getName();
			System.out.print("   " + name + "(");

			Class<?>[] paramTypes = c.getParameterTypes();
			for (int j = 0; j < paramTypes.length; j++) {
				if (j > 0)
					System.out.print("Par, ");
			}
			System.out.println(");");
		}
	}

	/**
	 * 方法显示
	 * 
	 * @param c1
	 */
	public static void printMethods(Class<?> c1) {
		Method[] methods = c1.getDeclaredMethods();
		System.out.println("------------------print methods ----------------");

		for (int i = 0; i < methods.length; i++) {
			Method m = methods[i];
			Class<?> retType = m.getReturnType();
			String name = m.getName();

			System.out.print(Modifier.toString(m.getModifiers()));
			System.out.println(" | " + retType.getName() + " |" + name + "(");

			Class<?>[] paramTypes = m.getParameterTypes();
			for (int j = 0; j < paramTypes.length; j++) {
				if (j > 0)
					System.out.print(", ");
				System.out.println(paramTypes[j].getName());
			}
			System.out.println("):");
		}
	}

	/**
	 * 变量参数显示
	 * 
	 * @param c1
	 */
	public static void printFields(Class<?> c1) {
		Field[] fields = c1.getDeclaredFields();
		AccessibleObject.setAccessible(fields, true);

		System.out.println("------------------print Fields ----------------");
		for (int i = 0; i < fields.length; i++) {
			Field f = fields[i];
			Class<?> type = f.getType();
			String name = f.getName();
			System.out.print(Modifier.toString(f.getModifiers()));
			System.out.println(" | " + type.getName() + "| " + name + ";");
		}
	}

	/**
	 * 显示对象内容
	 * 
	 * @param a
	 */
	public void printFields(Object a) {
		Class<?> c1 = a.getClass();

		Field[] fieldsDest = c1.getDeclaredFields();

		AccessibleObject.setAccessible(fieldsDest, true);

		System.out
				.println("------------------trans print Fields ----------------");
		// System.out.println("----fieldDest.length:"+fieldsDest.length);
		for (int i = 0; i < fieldsDest.length; i++) {
			Field f = fieldsDest[i];
			Class<?> type = f.getType();
			String name = f.getName();
			String typeName = type.getName();
			// System.out.println("[Time]::"+i+"[colname]:"+name+"[Typename]:"+typeName);
			if (name.equals("FIELDNUM") || name.equals("PK")
					|| name.equals("mErrors") || name.equals("fDate"))
				continue;
			// System.out.println("----fieldOrg.length:"+fieldsOrg.length);

			switch (transType(typeName)) {
			case 3:
				try {
					// System.out.println("***************float::"+name+"---"+f.getFloat(a));
					System.out.println("***************double::" + name + "---"
							+ f.getDouble(a));
				} catch (Exception e) {
					e.printStackTrace();
				}
				break;
			case 5:
				try {
					System.out.println("*************************Int::" + name
							+ "---" + f.getInt(a));
				} catch (Exception e) {
					e.printStackTrace();
				}
				break;
			case 93:
				try {
					System.out.println("*******************String::" + name
							+ "---" + f.get(a));
				} catch (Exception e) {
					e.printStackTrace();
				}
				break;
			default:
				try {
					System.out.println("------Default:" + f.get(a));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

		}

		System.out.println("-------- print end-------");

	}

	/**
	 * 类型转换
	 * 
	 * @param type
	 * @return
	 */
	private int transType(Object type) {
		int typecode;
		typecode = 93;
		if (type.equals("java.lang.String"))
			typecode = 93;
		// if (type.equals("float"))
		if (type.equals("double"))
			typecode = 3;
		if (type.equals("int"))
			typecode = 5;

		return typecode;
	}

	public boolean equals(Object otherobject) {
		if (this == otherobject)
			return true;

		if (otherobject == null)
			return false;

		if (getClass() != otherobject.getClass())
			return false;

		// Reflections other = (Reflections)otherobject;

		return true;
	}

	public String toString() {
		Class<?> ref = this.getClass();
		System.out.println("ref:" + ref);
		System.out.println("--------------------------");
		return getClass().getName();
	}

	public static void main(String[] args) {

	}

}